package com.gao.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.gao.domain.Cart;
import com.gao.domain.Order;
import com.gao.domain.OrderProduct;
import com.gao.domain.User;
import com.gao.service.CartService;
import com.gao.service.OrderProductService;
import com.gao.service.OrderService;
import com.gao.service.ProductService;
import com.gao.service.impl.CartServiceImpl;
import com.gao.service.impl.OrderProductServiceImpl;
import com.gao.service.impl.OrderServiceImpl;
import com.gao.service.impl.ProductServiceImpl;

/**
 * Servlet implementation class OrderAccountServlet
 */
@WebServlet("/OrderAccountServlet")
public class OrderAccountServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		int uid = user.getUid();
		CartService cartService = new CartServiceImpl();
		List<Cart> carts = cartService.getCartsByUid(uid);
		
		//���ɶ���
		OrderService orderService = new OrderServiceImpl();
		//ʹ��UUID����������
		int uuidHashcode = UUID.randomUUID().toString().hashCode();
		//uuidHashcode����Ǹ������������
		if(uuidHashcode<0){
			uuidHashcode=-uuidHashcode;
		}
		//0��ʾǰ�油��0��15���೤�ȣ�d��������������
		String oid = String.format("%015d", uuidHashcode);
		//�����ܼ�
		double amount = 0;
		for(Cart cart:carts){
			amount = cart.getCartNum()*cart.getProduct().getPrice();
		}
		//��ӽ�������
		orderService.add(new Order(oid,new User(uid),amount));
				
		//ɾ�����ﳵ����
		for(Cart cart:carts){
			cartService.deleteByCid(cart.getCid());
		}
		
		//��Ӧ��Ʒ������
		ProductService productService = new ProductServiceImpl();
		for(Cart cart:carts){
			productService.outProductById(cart.getProduct().getPid(), cart.getCartNum());
		}
		
		//��Ӷ�����Ʒ����
		OrderProductService orderProductService = new OrderProductServiceImpl();
		for(Cart cart:carts){
			OrderProduct orderProduct = new OrderProduct(new Order(oid), cart.getProduct().getPid(), cart.getProduct().getName(), cart.getProduct().getPrice(), cart.getCartNum());
			orderProductService.add(orderProduct);
		}
		
		//��ѯ�û����еĶ�����Ʒ��Ϣ
		List<OrderProduct> orderProducts = new ArrayList<OrderProduct>();

		List<Order> orders = orderService.getOrdersByUid(uid);
		for(Order order:orders){
			//ע�⣺��ӵ����������� addAll
			orderProducts.addAll(orderProductService.findByOid(order.getOid()));
		}
		//�Ѷ�������ȥ״̬
		for(OrderProduct orderProduct:orderProducts){
			String state = orderService.getStateByOid(orderProduct.getOrder().getOid());
			orderProduct.getOrder().setState(state);
		}
		
		request.setAttribute("orderProducts", orderProducts);
		request.getRequestDispatcher("order.jsp").forward(request, response);
	
	}

}
